package edu.scau.mis.sys.service.impl;

import edu.scau.mis.sys.entity.SysAdmin;
import edu.scau.mis.sys.entity.SysRole;
import edu.scau.mis.sys.mapper.SysAdminMapper;
import edu.scau.mis.sys.mapper.SysRoleMapper;
import edu.scau.mis.sys.mapper.SysUserRoleMapper;
import edu.scau.mis.sys.service.SysAdminService;
import edu.scau.mis.sys.vo.SysAdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysAdmin)表服务实现类
 *
 * @author makejava
 * @since 2022-03-02 23:15:34
 */
@Service("sysAdminService")
public class SysAdminServiceImpl implements SysAdminService {
    @Resource
    private SysAdminMapper sysAdminMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysAdmin queryById(Long id) {
        return this.sysAdminMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysAdmin 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysAdmin> queryByPage(SysAdmin sysAdmin, PageRequest pageRequest) {
        long total = this.sysAdminMapper.count(sysAdmin);
        return new PageImpl<>(this.sysAdminMapper.queryAllByLimit(sysAdmin, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public SysAdmin insert(SysAdmin sysAdmin) {
        this.sysAdminMapper.insert(sysAdmin);
        return sysAdmin;
    }

    /**
     * 修改数据
     *
     * @param sysAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public SysAdmin update(SysAdmin sysAdmin) {
        this.sysAdminMapper.update(sysAdmin);
        return this.queryById(sysAdmin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysAdminMapper.deleteById(id) > 0;
    }

    /**
     * 通过username查询单条数据
     *
     * @param userName
     * @return
     */
    @Override
    public SysAdmin queryByUsername(String userName){return this.sysAdminMapper.queryByUsername(userName);}

    /**
     * 修改密码
     *
     * @param id
     * @param password
     * @return
     */
    @Override
    public int updateByPassword(long id, String password){return this.sysAdminMapper.updateByPassword(id, password);}

    /**
     * 新增后台用户
     *
     * @param sysAdminVo
     */
    @Override
    public int insertAdmin(SysAdminVo sysAdminVo){

        sysAdminVo.setPassword(passwordEncoder.encode(sysAdminVo.getPassword()));// 密码加密
        return sysAdminMapper.insertSysAdmin(sysAdminVo);

    }

    /**
     * 修改用户，仅限超级管理员
     *
     * @param sysAdminVo
     * @return
     */
    @Override
    public int updateSysAdminWithSuper(SysAdminVo sysAdminVo){return sysAdminMapper.updateSysAdminWithSuper(sysAdminVo);}
}
